package com.cabal.common.util;

import lombok.SneakyThrows;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 字符串工具类工具类
 *
 * @author shenkang
 * @date 2022年11月24日
 */
public class TmsStringUtils {

	/**
	 * 将元数据前补零，补后的总长度为指定的长度，以字符串的形式返回
	 * 
	 * @param sourceNum
	 *            源数字
	 * @param formatLength
	 *            格式化长度
	 * @return
	 */
	public static String frontCompWithZore(long sourceNum, long formatLength) {
		return String.format("%0" + formatLength + "d", sourceNum);
	}
	
	/**
	 * 将元数据前补零，补后的总长度为指定的长度，以字符串的形式返回
	 * 
	 * @param sourceStr
	 *            源数字
	 * @param formatLength
	 *            格式化长度
	 * @return
	 */
	public static String frontStrCompWithZore(String sourceStr, int formatLength) {
		StringBuffer sb = new StringBuffer();
		int rs = formatLength-sourceStr.length();
		for(int i=0;i<rs;i++){
			sb.append("0");
		}
		sb.append(sourceStr);
		return sb.toString();
	}
	
	
	/**
	 * 替换姓名中间部分为*号进行保密
	 * @param realname
	 * @return
	 */
	public static String secrecyFullName(String realname){
//        String mobile = "15888888888";    
        String realnameRs =null;
        char[] r =  realname.toCharArray(); 
//        char[] m =  mobile.toCharArray();
          if(r.length ==1){
        	  realnameRs =  realname;
           }
           if(r.length == 2){
        	   realnameRs =  realname.replaceFirst(realname.substring(1),"*");
           }
           if (r.length > 2) {
        	   realnameRs =  realname.replaceFirst(realname.substring(1,r.length-1) ,"*");
           }
//           for(int i=0; i<m.length;i++){
//               if(i>2 && i<7){
//                   m[i] = '*';
//               }
//           }
//           String mobile1 =  String.valueOf(m);
//           System.out.println("姓名替换结果:"+realnameRs);//程*员
//           System.out.println(mobile1);//158****8888
		return realnameRs;
	}

	
	/**
	 * 替换null空白字符串,否则返回该字符串
	 */
	public static String replaceNullToEmpty(String str){
		if(str == null){
			return "";
		}
		return str;
	}
	
	/**
	 * 替换大部分空白字符， 不限于空格 \s 可以匹配空格、制表符、换页符等空白字符的其中任意一个 
	 * 
	 * @param str
	 * @return
	 */
	public static String trimAllBlank(String str){
		return str.replaceAll("\\s*", "");
	}
	

	/**
	 * 字符串去重
	 * 
	 * @param s
	 * @return
	 */
    public static String removeRepeatChar(String s) {
        if (s == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        int i = 0;
        int len = s.length();
        while (i < len) {
            char c = s.charAt(i);
            sb.append(c);
            i++;
            while (i < len && s.charAt(i) == c) {//这个是如果这两个值相等，就让i+1取下一个元素
                i++;
            }
        }
        return sb.toString();
    }

    public static String matchStr(String source1, String source2) {
    	int j;
		if(source1.length() > source2.length()) {
			j = source2.length();
		} else {
			j = source1.length();
		}

		StringBuffer buffer =  new StringBuffer();
		for(int i = 0; i < j; i++) {
			if(source1.charAt(i) == source2.charAt(i)) {
				buffer.append(source2.charAt(i));
				continue;
			}
			break;
		}
		return buffer.toString();
	}

	public static String getLogisticsChannelCode(String lcmCode, String logisticsChannelName) {
		String[] pinyinResult = TmsPinyinUtil.getHeadByString(logisticsChannelName, true);
		StringBuffer buffer = new StringBuffer();
		buffer.append(lcmCode);
		buffer.append("_");
		buffer.append("ESM");
		buffer.append("_");
		for(String pinyin : pinyinResult) {
			buffer.append(pinyin);
		}
		return buffer.toString();
	}

	public static String getRemoteAreaCode(String remoteAreaName) {
		String[] pinyinResult = TmsPinyinUtil.getHeadByString(remoteAreaName, true);
		StringBuffer buffer = new StringBuffer();
		for(String pinyin : pinyinResult) {
			buffer.append(pinyin);
		}
		return buffer.toString();
	}

	public static String stringSort(String s) {
		char [] charNumber = new char[s.length()];

		for(int m = 0; m < s.length(); m++) {
			charNumber[m] = s.charAt(m);
		}

		for (int i = 0; i < charNumber.length; i++) {
			for(int j=0; j<charNumber.length-i-1; j++){
				if(charNumber[j] > charNumber[j+1]){
					char temp = charNumber[j];
					charNumber[j] = charNumber[j+1];
					charNumber[j+1] = temp;
				}
			}
		}

		StringBuffer buffer = new StringBuffer(1024);
		for(int m = 0; m < charNumber.length; m++) {
			buffer.append(charNumber[m]);
		}
		return buffer.toString();
	}

	/**
	 * String数组是否包含某字段
	 *
	 * @param StringList
	 * @param content
	 * @return
	 */
	public static boolean contains(String[] StringList, String content) {

		if (content.equals("") || StringList.length == 0) {
			return false;
		}

		boolean isContains = false;

		for (int i = 0; i < StringList.length; i++) {
			if (content.equals(StringList[i])) {
				isContains = true;
			}
		}

		return isContains;
	}

	/**
	 * 将字符串数组变为字典顺序排序后的字符串数组
	 *
	 * @param sourceArr
	 * @return
	 */
	public static String[] stringSort(String[] sourceArr) {
		List<String> list = new ArrayList<String>(sourceArr.length);
		for (int i = 0; i < sourceArr.length; i++) {
			list.add(sourceArr[i]);
		}
		Collections.sort(list);
		return list.toArray(sourceArr);
	}
	@SneakyThrows
	public static String readToStreamToString(InputStream is) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] buffer = new byte[2048];
		int bytesRead;
        /*
        这一步可能会卡住,因为客户端如果传输完数据以后,
        如果没有调用socket.shutdownOutput()方法,会导致服务端不知道流是否已传输完毕,
        等待我们之前设置的10S流读取时间后,连接就会被自动关掉
        如果出现这种情况,服务端可以通过其它方式判断。例如换行符或者特殊字符等,只需要在
        while条件中加一个&&判断即可.例如我这里的业务结束标记是字符: ">",那么判断逻辑如下
        若客户端调用了shutdownOutput()方法,则不需要这个判断
        */
		while (!bos.toString().contains(">") && (bytesRead = is.read(buffer)) != -1) {
			bos.write(buffer, 0, bytesRead);
		}
		return bos.toString();
	}

	public static void main(String[] args) {
//		long sourceNum = 99999999;
//		long formatLength = 10;
//		String str = LmsStringUtils.frontCompWithZore(sourceNum, formatLength);
//		System.out.println("formate str-->" + str);
//		System.out.println(LmsStringUtils.secrecyFullName("魏学具您好啊魏学具您好啊"));
		
//		String str = frontStrCompWithZore("",8);
//		System.out.println(str);

		//System.out.println("北京 xx区".lastIndexOf("北京"));
		//System.out.println(getLogisticsChannelCode("WHER","威盛国际速递"));
		//System.out.println(stringSort("sfdsf12312313{1}"));

		String str = "158555603212332171516487ae1aaaf4-5134-1588-1228-999019d17d6eHZ20190229{\"storageName\":\"悉尼仓\",\"wayBillCode\":\"GVTKOA10000000\",\"customerOrderNo\":null,\"weight\":null,\"totalQuantityOrder\":\"1\",\"grandTotal\":\"0\",\"orderType\":\"直邮\",\"orderStatus\":null,\"billingName\":\"SK\",\"billingMobile\":\"15711956603\",\"billingAddress\":\"CAPalo Alto385 Sherman Ave.\",\"consigneeName\":\"KK\",\"consigneeMobile\":\"15711956607\",\"consigneeIdcard\":\"\",\"consigneePostCode\":\"455003\",\"provinceName\":\"广东省\",\"cityName\":\"深圳市\",\"regionName\":\"南山区\",\"consigneeAddress\":\"南山街道GVT阿波罗事业部\",\"actualWeight\":\"2.57\",\"remark\":null,\"grossWeight\":null,\"wayBillDetails\":[{\"productCode\":\"9421902960031\",\"quantityOrder\":\"1\",\"salePrice\":\"20\",\"rowTotal\":null,\"weight\":null,\"grossWeight\":\"2.57\",\"smallUnit\":\"850g\"}]}";
		/*try {
			String sign= LmsMD5Utils.md5(str).toUpperCase();
			System.out.println(sign);
		} catch (Exception e) {

		}*/



		System.out.println("-->" + stringSort(str));

	}
	
}
